package code.main.controller.table
{
	import code.app.AppDmList;
	import code.app.AppState;
	import code.app.AppView;
	import code.main.modle.lang.Language;
	import code.main.modle.table.Player;
	import code.main.view.table.BuyInDlgView;
	
	import com.yy.base.App;
	import com.yy.manager.UiManager;
	import com.yy.tools.string.StringUtil;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class BuyInDlgCtrl
	{
		private var _buyInDlgView:BuyInDlgView;
		private var _cb:Function;
		private var _player:Player;
		private var _chipsValue:int = 0;//买入的值
		public function BuyInDlgCtrl()
		{
			_player = AppDmList.getInstance().player;
		}
		
		public function getView(cb:Function):BuyInDlgView {
			_cb = cb;
			_buyInDlgView = new BuyInDlgView();
			_buyInDlgView.gameTips.text = StringUtil.substitute(Language.ROOM_BUY_IN_TIPS[0], StringUtil.getDollarFormat(_player.tableVo.bet));
			_buyInDlgView.restMoney.text = StringUtil.getDollarFormat(_player.gameMoney);
			_buyInDlgView.minText.text = StringUtil.getDollarFormat(_player.tableVo.minCarring);
			_buyInDlgView.maxText.text = StringUtil.getDollarFormat(_player.tableVo.maxCarring);
			_chipsValue = _player.tableVo.maxCarring/2;
			_buyInDlgView.chipsCount.text = StringUtil.getDollarFormat(_chipsValue);
			addEvent();
			return _buyInDlgView;
		}
		
		private function addEvent():void {
			_buyInDlgView.addEventListener(Event.ADDED_TO_STAGE, addToStageFunc);
			_buyInDlgView.closeBtn.addEventListener(MouseEvent.CLICK, clickCloseFunc);
			_buyInDlgView.chipsCount.addEventListener(Event.CHANGE, inputChangeFunc);
			_buyInDlgView.minBuyInBtn.addEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.maxBuyInBtn.addEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.buyBtn.addEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.increaseBtn.addEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.reduceBtn.addEventListener(MouseEvent.CLICK, clickBuyInFunc);
		}
		private function removeEvent():void {
			_buyInDlgView.removeEventListener(Event.ADDED_TO_STAGE, addToStageFunc);
			_buyInDlgView.closeBtn.removeEventListener(MouseEvent.CLICK, clickCloseFunc);
			_buyInDlgView.chipsCount.removeEventListener(Event.CHANGE, inputChangeFunc);
			_buyInDlgView.minBuyInBtn.removeEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.maxBuyInBtn.removeEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.buyBtn.removeEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.increaseBtn.removeEventListener(MouseEvent.CLICK, clickBuyInFunc);
			_buyInDlgView.reduceBtn.removeEventListener(MouseEvent.CLICK, clickBuyInFunc);
		}
		
		/**
		 * 点击按钮回调
		 * 
		 */
		private function clickBuyInFunc(e:Event):void {
			var obj:Object = e.currentTarget;
			switch(obj) {
				case _buyInDlgView.minBuyInBtn:
					_cb(_player.tableVo.minCarring);
					close();
					break;
				case _buyInDlgView.maxBuyInBtn:
					_cb(_player.tableVo.maxCarring);
					close();
					break;
				case _buyInDlgView.buyBtn:
					_cb(_chipsValue);
					close();
					break;
				default:
					if(obj == _buyInDlgView.reduceBtn) {
						_chipsValue -= _player.tableVo.minCarring;
					}else if(obj == _buyInDlgView.increaseBtn) {
						_chipsValue += _player.tableVo.minCarring;
					}
					_buyInDlgView.chipsCount.text = StringUtil.getDollarFormat(_chipsValue);
					_buyInDlgView.chipsCount.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		/**
		 *输入筹码 
		 * @param e
		 * 
		 */
		private function inputChangeFunc(e:Event):void {
			var value:int = getValue();
			autoEnterFous();
			var flag:Boolean = false;
			if(value < _player.tableVo.minCarring) {
				_buyInDlgView.gameTips.text = Language.ROOM_BUY_IN_TIPS[1];
				value = _player.tableVo.minCarring;
				flag = true;
			}
			if(value > _player.tableVo.maxCarring) {
				_buyInDlgView.gameTips.text = Language.ROOM_BUY_IN_TIPS[2];
				value = _player.tableVo.maxCarring;
				flag = true;
			}
			if(_player.gameMoney < value) {
				flag = true;
				_buyInDlgView.gameTips.text = Language.ROOM_BUY_IN_TIPS[3];
				value = _player.gameMoney;
			}
			_chipsValue = value;
			if(!flag) {
				_buyInDlgView.gameTips.text = StringUtil.substitute(Language.ROOM_BUY_IN_TIPS[0], StringUtil.getDollarFormat(_player.tableVo.bet));
			}
			_buyInDlgView.chipsCount.text = StringUtil.getDollarFormat(value);
		}
		/**
		 *活得输入的值 
		 * @return 
		 * 
		 */
		private function getValue():Number {
			return StringUtil.getDollarFormatValue(_buyInDlgView.chipsCount.text);
		}
		
		/**
		 *设置光标 
		 * 
		 */
		private function autoEnterFous():void{
			_buyInDlgView.chipsCount.setSelection(_buyInDlgView.chipsCount.text.length, _buyInDlgView.chipsCount.text.length);
			App.appStage.focus = _buyInDlgView.chipsCount;
		}
		
		/**
		 *添加到舞台 
		 * @param e
		 * 
		 */
		private function addToStageFunc(e:Event):void {
			_buyInDlgView.removeEventListener(Event.ADDED_TO_STAGE, addToStageFunc);
			_buyInDlgView.chipsCount.setSelection(0,_buyInDlgView.chipsCount.text.length);
			App.appStage.focus = _buyInDlgView.chipsCount;
		}
		
		/**
		 *点击关闭按钮 
		 * @param e
		 * 
		 */
		private function clickCloseFunc(e:Event):void {
			close();
		}
		private function close():void {
			removeEvent();
			UiManager.getPopUpManager().removePopUp(_buyInDlgView);
			_buyInDlgView = null;
			_cb = null;
		}
		
	}
}